﻿using System;
using System.Transactions;
using Castle.DynamicProxy;
using WS.EKA.Core.Attributes;
using WS.EKA.LogBridge;

namespace WS.EKA.Core.Interceptor
{
    class TansactionInterceptor
    {

        public static void Proceed(IInvocation invocation)
        {
            try
            {
                if (InterceptHelper.IsAttribute(typeof(TransactionAttribute), invocation))
                {
                    TransactionOptions options = new TransactionOptions();
                    //Set Isolation level is ReadCommitted,Default is Serializable
                    options.IsolationLevel = IsolationLevel.ReadCommitted;
                    //Set time out is 2 minutes,Default is 1 minutes
                    options.Timeout = new TimeSpan(0, 2, 0);
                    using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, options))
                    {
                        invocation.Proceed();
                        scope.Complete();
                    }
                }
                else
                {
                    invocation.Proceed();
                }
            }
            catch (Exception ex)
            {
                Log.Error(string.Format("Exception invoke method:{0}", invocation.Method), ex);
                throw;
            }
            finally
            {
            }
        }
    }
}
